package com.nirvana.utils;


import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Copyright (c) 2022 Aoshu.Co.Ltd. All rights reserved.
 *
 * @author :    zhouqichun
 * @create :    2025/11/12 15:20
 * @description :
 **/
public class CmdLineUtil {
    public static int exec(String cmd) {
        try {
            System.out.println("命令执行开始，cmd: " + cmd);
            Process process = Runtime.getRuntime().exec(cmd);

            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                reader.lines().forEach(line -> System.out.println("[OUT] " + line));
            }
            int exitCode = process.waitFor();
            if (exitCode == 0) {
                System.out.println("命令执行成功");
            } else {
                System.out.println("命令执行失败：" + exitCode);
            }
            return exitCode;
        } catch (Exception e) {
            System.out.println("命令执行结束，错误: " + e);
        }
        return -1;
    }
}
